<?php
namespace App\Http\Controllers\user;

use App\Engine\BaseController;
use App\Http\Controllers\service\XcxService;
use function app\Utils\go;
use App\Models\User as UserModel;
use App\Engine\TokenService;
use App\Models\FxLog as FxLogModel;
use App\Models\Sys as SysModel;
use App\Engine\WxCode\demoWxCode;
use Illuminate\Support\Facades\Log;

class User extends BaseController
{
    
    /**
     * 小程序获取token
     * */
     public function xcx_login()
    {

        $wxResult=(new XcxService())->login_xcx()->auth->session($this->post['code']);
        if(empty($wxResult)) {
            throw new ApiException('获取session_key及openID时异常，微信内部错误');
        }
        $loginFail = array_key_exists('errcode', $wxResult);
        
        if ($loginFail){
            throw new ApiException($loginFail['errmsg']);
        }
        if(isset($this->post['invite_id'])){
            $wxResult['invite_id']=$this->post['invite_id'];
        }else{
            $wxResult['invite_id']=0;
        }
       
        $res=UserModel::get_xcx_token($wxResult);
        return go($res);

    }
    
    /**
     * 获取个人信息
     * */
    public function infor()
    {
        $uid=TokenService::getCurrentUid();
        $res=UserModel::where('id',$uid)->first();
        return go($res);
        
    }
    
        /**
     * 获取邀请用户
     * */
    public function get_invite_user()
    {
           $uid=TokenService::getCurrentUid();
           $res=UserModel::where('invite_id',$uid)->get();
           return go($res);
    }
    
    public function bind_mobile()
    {
        $uid=TokenService::getCurrentUid();
        $user=UserModel::where('id',$uid)->first();
        $res=(new demoWxCode())->decryptData($this->post['encryptedData'],$this->post['iv'],$user['session_key']);
        if($user['mobile']==''){
            $invite_id=$user['invite_id'];
            $user->mobile=$res['phoneNumber'];
            $res=$user->save();
            if($invite_id>0){
                $bind_mobile_money=SysModel::getValue('bind_mobile_money');
                UserModel::where('id',$this->get['invite_id'])->increment('money',$bind_mobile_money);
            }
        }
         return go($res);
    }
    
    /**
     * 绑定用户
     * */
    public function bind_user()
    {
        $uid=TokenService::getCurrentUid();
        $user=UserModel::where('id',$uid)->first();
        if($this->get['invite_id']==$uid){
              return go(['data'=>true]); 
        }
        
        if($user['invite_id']==0||$user['invite_id']==''){
            $user->invite_id=$this->get['uid'];
            $res=$user->save();
            $invite_money=SysModel::getValue('invite_money');
            UserModel::where('id',$this->get['invite_id'])->increment('money',$invite_money);
            
            return go($res);
        }
        
        return go(['data'=>true]);
    }
    
    /**
     * 更新个人信息
     * */
    public function update_infor()
    {
       $data=[
           'nickname'=>$this->post['nickName'],
           'headpic'=>$this->post['avatarUrl']
       ];
       return go(UserModel::find(TokenService::getCurrentUid())->update($data));
    }
    
    public function tx()
    {
        $user=UserModel::where('id',TokenService::getCurrentUid())->first();
        if($user['money']<$this->post['money']){
            throw new ApiException('余额不足');
        }
        
        $res=$user->decrement('money',$this->post['money']);
        return go($res);
    }
    
    
    /**
     * 获取分销日志
     * */
    public function get_fx_log()
    {
        $res=FxLogModel::with('user')->where('agent',TokenService::getCurrentUid())->orderBy('id','desc')->get();
        return go($res);
    }
    
    
    
    /**
     * 
     * */
    public function create_house_qrcode()
    {
        $data=[
            'uid'=>TokenService::getCurrentUid(),
            'house_id'=>$this->get['id'],
            'type'=>'house_code'
            ];
        
        $parms="uid={$data['uid']}&house_id={$data['house_id']}&type={$data['type']}";
        $res=(new XcxService())->crate_xcx_qrcode($parms);
        return go($res);
        
    }
    
}